package com.example.ggpicturegg;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ggpicturegg.Mysql_com.ImageInfo;
import com.example.ggpicturegg.Mysql_com.Mysql_tool;
import com.example.ggpicturegg.first.RecyclerviewActivity;
import com.example.ggpicturegg.ftpUD.UpAndDown;
import com.example.ggpicturegg.imageinfo.UserNameAndImageInfo;
import com.example.ggpicturegg.imageinfo.UserNameAndNameinfo;
import com.example.ggpicturegg.tool.CommonUtils;
import com.example.ggpicturegg.tool.Loaddata;

import java.io.UnsupportedEncodingException;
import java.util.List;

public class Imageshow extends AppCompatActivity {

    private TextView show_uname;
    private TextView show_utitle;
    private TextView show_udesc;
    private ImageView show_tou;
    private ImageView download_;
    private ImageView care_user;
    private ImageView like_image;
    private ImageView image_btn_return;
    private ImageView Tv_imageView ;
    private Bitmap bitmap_temp;
    private ImageInfo imageInfos;

    private String path;    // 图片 FTP 上的名字
    private String sd_file;  // 图片的生成的名字

    private Boolean like_f = false;
    private Boolean care_f = false;
    private String username;
    private int image_id;
    private String image_name;
    private Mysql_tool tool;
    private String judge_name;

    private Handler mainHandler;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imageshow);
        intiView();



        Bundle bundletemp = getIntent().getExtras();
        username  = "";
        username = bundletemp.getString("UserName");

        // 将图片的信息找出来
        imageInfos = findImageinfo();
        // 展示信息
        show_uname.setText(imageInfos.getX_name());
        show_utitle.setText(imageInfos.getX_title());
        show_udesc.setText(imageInfos.getDesc());
        image_id = imageInfos.getId();
        image_name =imageInfos.getX_name();


        // 查看是否需要 切换 图标

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("----username---: "+username+"------------image_id: "+image_id+"----------");
                final UserNameAndImageInfo nameAndImageInfo = tool.find_is_like(username,image_id);
                final UserNameAndNameinfo nameAndNameinfo = tool.find_is_care(username,image_name);
                if(nameAndImageInfo!=null){
                    like_f = true;
                }
                if(nameAndNameinfo!=null){
                    care_f = true;
                }

                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if(!like_f){
                            like_image.setImageResource(R.drawable.like_no);
                        }else{
                            like_image.setImageResource(R.drawable.like_yes);
                        }

                        if(!care_f){
                            care_user.setImageResource(R.drawable.care_no);
                        }else{
                            care_user.setImageResource(R.drawable.care_yes);
                        }
                    }
                });
            }
        }).start();

        // 展示照片
        new Thread(new Runnable() {
            @Override
            public void run() {

                bitmap_temp =  Loaddata.getImage(imageInfos.getPicture_name(),imageInfos.getId());

                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        Tv_imageView.setImageBitmap(bitmap_temp);
                    }
                });
            }
        }).start();


        //judge();

    }

    private void judge(){
        Mysql_tool tool = new Mysql_tool();
        new Thread(new Runnable() {
            @Override
            public void run() {
                judge_name = tool.getname(image_name);

                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (judge_name == image_name){
                            System.out.println("printf(-----数据库有这个名字的图片---   "+judge_name+"    ---------)");
                            showhead();
                        }
                        if(judge_name==""){
                            System.out.println("printf(-----数据库没有这个名字的图片---   "+judge_name+"    ---------)");
                        }
                    }
                });
            }
        }).start();

    }
    private void showhead(){
        //  图片展示
        new Thread(new Runnable() {
            @Override
            public void run() {

                Bitmap bitmap =  Loaddata.getImage(image_name+".jpg",0);
                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        show_tou.setImageBitmap(bitmap);
                    }
                });
            }
        }).start();
    }
    private void intiView(){
        show_uname = findViewById(R.id.show_name);
        show_tou = findViewById(R.id.show_touxiang);
        show_utitle = findViewById(R.id.show_title);
        show_udesc = findViewById(R.id.show_desc);

        download_ = findViewById(R.id.download);
        care_user = findViewById(R.id.care_user);
        like_image = findViewById(R.id.like_image);
        image_btn_return = findViewById(R.id.image_btn_return);
        Tv_imageView = findViewById(R.id.show_image_all);

        OnClick onClick = new OnClick();
        tool = new Mysql_tool();

        download_.setOnClickListener(onClick);
        care_user.setOnClickListener(onClick);
        show_tou.setOnClickListener(onClick);
        like_image.setOnClickListener(onClick);
        image_btn_return.setOnClickListener(onClick);
        mainHandler = new Handler(getMainLooper());
    }


    private ImageInfo findImageinfo(){
        ImageInfo item= new ImageInfo();
        Bundle bundle = getIntent().getExtras();
        item.setId(bundle.getInt("id"));
        item.setX_name(bundle.getString("user_name"));
        item.setX_title(bundle.getString("title"));
        item.setPicture_name(bundle.getString("path"));
        item.setDesc(bundle.getString("desc"));
        return item;
    }

    private void download_image(){

        // 格式
        //                           /storage/emulated/0/Pictures/
        // String s = UpAndDown.ftpDown("/sdcard/Pictures/","9_image.jpg","9_image.jpg");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    path = "No."+imageInfos.getId()+"-"+imageInfos.getPicture_name();
                    System.out.println("$$$$$$$$$$         ------------          "+path);
                    sd_file = CommonUtils.Random_name()+".jpg";
                    String s = UpAndDown.ftpDown("/storage/emulated/0/Pictures/",path,sd_file);
                    if(s=="1"){
                        Looper.prepare();
                        Toast.makeText(Imageshow.this,"下载成功",Toast.LENGTH_SHORT).show();
                        CommonUtils.showShortMsg(Imageshow.this,"下载成功");
                        Looper.loop();
                    }else{
                        Looper.prepare();
                        Toast.makeText(Imageshow.this,"下载失败",Toast.LENGTH_SHORT).show();
                        CommonUtils.showShortMsg(Imageshow.this,"下载失败");
                        Looper.loop();
                    }

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    private void Care_User(){
        ////////////  关注博主

        // 取消关注
        if(care_f){
            //  已经关注了
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // 数据库操作 删除  关注博主
                    final int iRow = tool.delete_is_care(username,image_name);
                    //查看是否成功删除 关注
                    final UserNameAndNameinfo nameAndNameinfo = tool.find_is_care(username,image_name);
                    if(nameAndNameinfo==null){
                        care_f = false;
                    }

                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if(!care_f){
                                // 成功了
                                care_user.setImageResource(R.drawable.care_no);
                            }
                        }
                    });
                }
            }).start();

        }

        // 关注
        else{
            // 暂未关注的
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // 数据库操作 增加   关注博主
                    final int iRow = tool.add_is_care(username,image_name);
                    //查看是否关注成功
                    final UserNameAndNameinfo nameAndNameinfo = tool.find_is_care(username,image_name);
                    if(nameAndNameinfo!=null){
                        care_f = true;
                    }

                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if(care_f){
                                // 成功了
                                care_user.setImageResource(R.drawable.care_yes);
                            }
                        }
                    });
                }
            }).start();

        }
    }

    private void Like_Image(){
        ////////////  点赞照片

        // 取消点赞
        if(like_f){
            //  已经关注了
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // 数据库操作 删除  照片的喜欢
                    final int iRow = tool.delete_is_like(username,image_id);
                    //查看是否成功删除 关注
                    final UserNameAndImageInfo userNameAndImageInfo = tool.find_is_like(username,image_id);
                    if(userNameAndImageInfo==null){
                        like_f = false;
                    }

                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if(!like_f){
                                // 成功了
                                like_image.setImageResource(R.drawable.like_no);
                            }
                        }
                    });
                }
            }).start();

        }


        // 点赞
        else{
            // 暂未关注的
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // 数据库操作 增加   关注博主
                    final int iRow = tool.add_is_like(username,image_id);
                    //查看是否关注成功
                    final UserNameAndImageInfo userNameAndImageInfo = tool.find_is_like(username,image_id);
                    if(userNameAndImageInfo!=null){
                        like_f = true;
                    }

                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if(like_f){
                                // 成功了
                                like_image.setImageResource(R.drawable.like_yes);
                            }
                        }
                    });
                }
            }).start();

        }
    }

    class OnClick implements View.OnClickListener{
        Intent intent ;
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.show_image_all:
                    // 换头像

                    break;
                case R.id.download:
                    // 下载
                    download_image();

                    //finish();    // 结束当前页面
                    break;
                case R.id.care_user:
                    // 关注博主
                    Care_User();
                    break;
                case R.id.like_image:
                    // 点赞照片
                    Like_Image();
                    break;
                case R.id.image_btn_return:
                    finish();    // 结束当前页面
                    break;
            }

        }
    }

}